'''
Created on 2020年3月3日

@author: Administrator
'''
from orm.model import Notify,db
from view.baseview import BaseView
import time,datetime
from common.public import *

class NotifyView(BaseView):
    def __init__(self):
        pass

    def query(self, params):
        """根据条件取得通知列表，params：条件参数"""
        result=[]
        where_condition = True
        
        if params["keyword"]:
            likestr= "%"+params["keyword"] +"%"
            where_condition=(Notify.content ** likestr )
            where_condition=where_condition | (Notify.memo** likestr )
        if params["type"]:
            where_condition=where_condition &(Notify.type.in_(params["type"]))
        if params["status"]:
            where_condition=where_condition &(Notify.status.in_(params["status"]))
        if params["userid"]:
            where_condition=where_condition &(Notify.notifier_id==params["userid"])
        if params["createdatetime"]:
            likestr= params["createdatetime"] 
            where_condition=where_condition & ((Notify.createdatetime >= params["createdatetime"][0] + " 00:00:00") & (Notify.createdatetime <= params["createdatetime"][1] + " 23:59:59") )
        total = Notify.select().where(where_condition).count()
        rows = Notify.select().where(where_condition).order_by(Notify.updatetime.desc()).paginate(int(params["pageNo"]), int(params["pagesize"]))
        for row in rows:
            presserid = 0
            presser = '';
            if row.presser_id != None:
                presserid = presserid
                presser = row.presser.username
            data = {
                "id":row.id,
                "content":row.content,
                "memo":row.memo,
                "type":row.type,
               "createdatetime":str(row.createdatetime),
               "updatetime":str(row.updatetime),
               "presserid":presserid,
               "presser":presser,
               "trackerid":row.tracker.id,
               "trackername":row.tracker.username,
               "status":row.status
             }
            result.append(data )
        return super().resultSet(200, "", result, total=total)
    
         
    @db.atomic()                   
    def update(self, params):
        """
        更新
        """
        # 找到公共客户
        notify = Notify.get(Notify.id==params["id"])
        notify.memo=params["memo"]
        notify.status=NOTIFY_STATUS_PRESSED
        notify.updatetime= time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
        notify.presser= params["presserid"]
        notify.save()
        return super().resultSet(200, "更新成功", [])
        raise Exception("更新失败")
    
    @db.atomic()                   
    def batch(self, params):
        """
        批量处理
        """
        # 找到公共客户
        for id in params['ids']:
            notify = Notify.get(Notify.id==id)
            notify.memo=params["memo"]
            notify.status=NOTIFY_STATUS_PRESSED
            notify.updatetime= time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
            notify.presser= params["presserid"]
            notify.save()
        return super().resultSet(200, "更新成功", [])
        raise Exception("更新失败")
